#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
int main()
{
    vector<vector<int>> map(5, vector<int>(5, 0));
    srand(time(NULL));

    while (1)
    {
        int x = rand() % 5;
        int y = rand() % 5;
        //cout << x << ' ' << y << endl;
        while (map[x][y])
        {
            x = rand() % 5;
            y = rand() % 5;
        }
        map[x][y] = 2;
        system("cls");
        for (int i = 0; i < 5; ++i)
        {
            for (int j = 0; j < 5; ++j)
                cout << map[i][j] << "   ";
            cout << endl;
        }
        char option;
        int flag = 1;
        while(flag)
        {
        cin >> option;
        switch (option)
        {
        case 'w':
            for (int i = 1; i < 5; ++i)
            {
                for (int j = 0; j < 5; ++j)
                {
                    if (map[i][j]==0)
                        continue;
                    int k = i;
                    if (map[k - 1][j]==0)
                    {
                        while (k>0&&map[k - 1][j]==0)
                        {
                            map[k - 1][j] = map[k][j];
                            map[k][j] = 0;
                            --k;
                            flag = 0;
                        }
                    }
                    if (k>0&&map[k - 1][j] == map[k][j])
                    {
                        map[k - 1][j] += map[k][j];
                        map[k][j] = 0;
                        flag = 0;
                    }
                }
            }
            break;
        case 's':
            for (int i = 3; i >= 0; --i)
            {
                for (int j = 0; j < 5; ++j)
                {
                  if (map[i][j]==0)
                        continue;
                    int k = i;
                    if (map[k + 1][j]==0)
                    {
                        while (k<4&&map[k + 1][j]==0)
                        {
                            map[k + 1][j] = map[k][j];
                            map[k][j] = 0;
                            ++k;
                            flag = 0;
                        }
                    }
                    if (k<4&&map[k + 1][j] == map[k][j])
                    {
                        map[k + 1][j] += map[k][j];
                        map[k][j] = 0;
                        flag = 0;
                    }  
            }
            }
            break;
        case 'a':
        for (int i = 0; i < 5; ++i)
            {
                for (int j = 1; j < 5; ++j)
                {
                    if (map[i][j]==0)
                        continue;
                    int k = j;
                    if (map[i][j-1]==0)
                    {
                        while (k>0&&map[i][k-1]==0)
                        {
                            map[i][k-1] = map[i][k];
                            map[i][k] = 0;
                            --k;
                            flag = 0;
                        }
                    }
                    if (k>0&&map[i][k-1] == map[i][k])
                    {
                        map[i][k-1] += map[i][k];
                        map[i][k] = 0;
                        flag = 0;
                    }
                }
            }
            break;
        case 'd':
        for (int i = 0; i < 5; ++i)
            {
                for (int j = 3; j >=0; --j)
                {
                    if (map[i][j]==0)
                        continue;
                    int k = j;
                    if (map[i][j+1]==0)
                    {
                        while (k<4&&map[i][k+1]==0)
                        {
                            map[i][k+1] = map[i][k];
                            map[i][k] = 0;
                            ++k;
                            flag = 0;
                        }
                    }
                    if (k<4&&map[i][k+1] == map[i][k])
                    {
                        map[i][k+1] += map[i][k];
                        map[i][k] = 0;
                        flag = 0;
                    }
                }
            }
            break;
        default:
            break;
            }
        if(flag)
            cout << "请输入能移动的步骤" << endl;
        }
    }
}